package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.My_Product_Adapter;
import com.example.xianji.BGARefresh.BGANormalRefreshViewHolder;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.Service;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main_Indefine_FenLei_LieBiaoActivity extends Activity implements BGARefreshLayoutDelegate {
    private BGARefreshLayout my_indefine_fenlei_RefreshLayout;
    private ListView my_indefine_fenlei_listview;
    private My_Product_Adapter mMy_Product_Adapter;
    private ArrayList<Service> service_list = new ArrayList<Service>();
    //    private SharedPreferences sp;
//    private Editor ed;
    private RelativeLayout relativelayout_product_fenlei_back;
    private int page = 1;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                my_indefine_fenlei_listview.setAdapter(mMy_Product_Adapter);
                my_indefine_fenlei_RefreshLayout.endRefreshing();
            }
            if (msg.what == 2) {
                mMy_Product_Adapter.notifyDataSetChanged();
                my_indefine_fenlei_RefreshLayout.endLoadingMore();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__indefine__fen_lei__lie_biao);

        my_indefine_fenlei_RefreshLayout = (BGARefreshLayout) findViewById(R.id.my_indefine_fenlei_RefreshLayout);
        my_indefine_fenlei_listview = (ListView) findViewById(R.id.my_indefine_fenlei_listview);
        my_indefine_fenlei_RefreshLayout.setDelegate(this);
        my_indefine_fenlei_RefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(this, true));
        mMy_Product_Adapter = new My_Product_Adapter(this, service_list, Contant.product_come_from_indefine_fenlei);
        relativelayout_product_fenlei_back = (RelativeLayout) findViewById(R.id.relativelayout_product_fenlei_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        get_fenlei();
        relativelayout_product_fenlei_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Indefine_FenLei_LieBiaoActivity.this.finish();
            }
        });
    }

    public void get_fenlei() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        map.put("token", sp.getString("xianji_token", ""));
        map.put("page", 1 + "");
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.indefine_fenlei_liebiao,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        service_list.clear();
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() == 0) {
                            Toast.makeText(Main_Indefine_FenLei_LieBiaoActivity.this, "您还没有创建分类哦", Toast.LENGTH_SHORT).show();
                            my_indefine_fenlei_RefreshLayout.endRefreshing();
                        } else {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject OBJ = array.getJSONObject(i);

                                Service mService = new Service();
                                mService.setId(OBJ.getInt("id"));
                                mService.setParent(OBJ.getInt("parent"));//父级分类
                                mService.setTitle(OBJ.getString("name"));//分类名称
                                mService.setParent_name(OBJ.getString("parent_name"));
                                service_list.add(mService);
                            }
                            handler.sendEmptyMessage(1);
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    public void get_fenlei_more() {
        page++;
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        map.put("token", sp.getString("xianji_token", ""));
        map.put("page", page + "");
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.indefine_fenlei_liebiao,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() == 0) {
                            Toast.makeText(Main_Indefine_FenLei_LieBiaoActivity.this, "您还没有创建分类哦", Toast.LENGTH_SHORT).show();
                            my_indefine_fenlei_RefreshLayout.endLoadingMore();
                        } else {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject OBJ = array.getJSONObject(i);
                                Service mService = new Service();
                                mService.setId(OBJ.getInt("id"));
                                mService.setParent(OBJ.getInt("parent"));//父级分类
                                mService.setTitle(OBJ.getString("name"));//分类名称
                                mService.setParent_name(OBJ.getString("parent_name"));
                                service_list.add(mService);
                            }
                            handler.sendEmptyMessage(2);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        get_fenlei();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(
            BGARefreshLayout refreshLayout) {
        get_fenlei_more();
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Contant.product_fenlei_editor_finish == 1) {
            page = 1;
            get_fenlei();
            Contant.product_fenlei_editor_finish = 0;
        }
    }
}
